// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Xml;


namespace svs.Mobile.BeyondScheduler.Core
{
	public class Configuration
	{
		private static readonly string m_AppInstallPath;
		private static readonly NameValueCollection m_settings = new NameValueCollection();
		public static readonly string FallBackInstallPath = @"\Program Files";

		static Configuration()
		{
			m_AppInstallPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
			if (m_AppInstallPath.StartsWith(Uri.UriSchemeFile, StringComparison.CurrentCultureIgnoreCase))
				m_AppInstallPath = m_AppInstallPath.Substring(6);

			if (string.IsNullOrEmpty(m_AppInstallPath) || !Directory.Exists(m_AppInstallPath))
			{
				m_AppInstallPath = FallBackInstallPath;
				CoreHelper.WriteLogEntry("Unable to read the default application Install folder!");
			}

			// Get the path of the settings file.
			string settingsPath = ApplicationInstallPath + @"\Settings.xml";

			if (File.Exists(settingsPath))
			{
				try
				{
					XmlDocument xdoc = new XmlDocument();
					xdoc.Load(settingsPath);
					XmlElement root = xdoc.DocumentElement;
					XmlNodeList nodeList = root.ChildNodes.Item(0).ChildNodes;

					// Add settings to the NameValueCollection.
					foreach (XmlNode n in nodeList)
					{
						m_settings.Add(n.Attributes["key"].Value, n.Attributes["value"].Value);
					}
				}
				catch (Exception ex)
				{
					CoreHelper.WriteLogEntry("Unable to read application settings! Reason:" + ex.Message);
				}
			}
		}

		public static string LogFilePath
		{
			get { return ApplicationInstallPath + @"\BeyondSchedulerLog.txt"; }
		}

		public static string SheduleRunnerPath
		{
			get
			{
				return ApplicationInstallPath + @"\ScheduleRunner.exe";
				//return ApplicationInstallPath + @"\NativeScheduleRunner.exe";
			}
		}

		public static string RepositoryFilePath
		{
			get
			{
				return ApplicationInstallPath + @"\ScheduledTasks.xml";
			}
		}

		public static string ProductVersion
		{
			get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
		}

		public static string ApplicationInstallPath
		{
			get { return m_AppInstallPath; }
		}

		/// <summary>
		/// Gets or sets the actively selected category
		/// </summary>
		public static string FeedCategories
		{
			get
			{
				var categories = m_settings.Get("FeedCategories");
				return string.IsNullOrEmpty(categories) ?
					"News|Technology|Entertainment|Software|Favorites|Podcast Directories" :
					categories;
			}
		}

	}
}